package com.prs.crm.service.cdxh.impl;

import java.util.ArrayList;
import java.util.List;

import com.prs.crm.dao.cdxh.DefineSalaryDao;
import com.prs.crm.domain.cdxh.HrPerson;
import com.prs.crm.domain.cdxh.salary.DefineSalary;
import com.prs.crm.domain.cdxh.wages.MaterialWages;
import com.prs.crm.domain.cdxh.wages.MoldingWages;
import com.prs.crm.domain.cdxh.wages.PrintWages;
import com.prs.crm.service.cdxh.DefineSalaryService;
import com.prs.crm.service.cdxh.HrPersonService;
import com.prs.crm.service.cdxh.MaterialWagesService;
import com.prs.crm.service.cdxh.MoldingWagesService;
import com.prs.crm.service.cdxh.PrintWagesService;
import com.prs.crm.service.cdxh.ProvideRegimeService;
import com.prs.crm.service.impl.BaseServiceImpl;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-17 上午10:40:53 简单说明
 */

public class DefineSalaryServiceImpl extends
		BaseServiceImpl<DefineSalary, DefineSalaryDao> implements
		DefineSalaryService {
	private ProvideRegimeService provideRegimeService;
	
	private MaterialWagesService materialWagesService;
	
	private MoldingWagesService  moldingWagesService;
	
	private PrintWagesService  printWagesService;

	private DefineSalaryService defineSalaryService;
	
	private HrPersonService hrPersonService;

	/**
	 * 保存工资表
	 */
	@Override
	public void saveDefineSalary(DefineSalary defineSalary, String persons) {
		/**
		 * 人員拆分
		 */
	System.out.println("persons=" + persons);
		String rowdaPerson[] = persons.split(",");
		int perosnLength = rowdaPerson.length;
		String montsh = defineSalary.getSalMonth() + "/"
				+ defineSalary.getSalYear();
		 System.out.println("时间=" + montsh);
	     System.out.println("所属组=" + defineSalary.getSalTeam());
		if (perosnLength == 0) {
			HrPerson hpObject = this.getHrPersonService().getHrPersonMoney(persons.trim(), defineSalary.getSalTeam(), montsh);
			if (hpObject != null) {
				defineSalary.setRewardsPunishment(hpObject.getRewardNum()- hpObject.getPoundNum());
			} else {
				defineSalary.setRewardsPunishment(0.0);
			}
			defineSalary.setSalPersonName(persons);
			defineSalary.setRegistState(1);
			this.getDefineSalaryService().save(defineSalary);
		} else {
			for (String perons : rowdaPerson) {
				DefineSalary defineOnbJECT = new DefineSalary();
				HrPerson hpObject = this.getHrPersonService().getHrPersonMoney(persons.trim(), defineSalary.getSalTeam(), montsh);
				if (hpObject != null) {
					defineOnbJECT.setRewardsPunishment(hpObject.getRewardNum()- hpObject.getPoundNum());
				} else {
					defineOnbJECT.setRewardsPunishment(0.0);
				}
				defineOnbJECT.setSalYear(defineSalary.getSalYear());
				defineOnbJECT.setSalMonth(defineSalary.getSalMonth());
				defineOnbJECT.setSalTeam(defineSalary.getSalTeam());
			System.out.println("defineSalary.getSalCriterion()="+defineSalary.getSalCriterion());
				defineOnbJECT.setSalCriterion(defineSalary.getSalCriterion());
				defineOnbJECT.setSalPersonName(perons.trim());
				defineOnbJECT.setRegistState(1);
			System.out.println("defineOnbJECT=" + defineOnbJECT);
				this.getDefineSalaryService().save(defineOnbJECT);
			}
		}

	}

	public DefineSalaryService getDefineSalaryService() {
		return defineSalaryService;
	}

	public void setDefineSalaryService(DefineSalaryService defineSalaryService) {
		this.defineSalaryService = defineSalaryService;
	}

	public HrPersonService getHrPersonService() {
		return hrPersonService;
	}

	public void setHrPersonService(HrPersonService hrPersonService) {
		this.hrPersonService = hrPersonService;
	}

	/**
	 * 工资单登记
	 */
	@Override
	public void saveAlldefineSalarys(DefineSalary defineSalary,
			Double productMoney, String[] types, Double[] prices) {
		// System.out.println("DefineSalary="+defineSalary);
		double totalSalar = 0.0;
		int intlength = prices.length;
		for (int i = 0; i < intlength; i++) {
			if ("减项".equals(types[i])) {
				totalSalar = totalSalar - prices[i];
			} else {
				totalSalar = totalSalar + prices[i];
			}
		}

		totalSalar = totalSalar + productMoney;

		DefineSalary dsObject = this.getDefineSalaryService().get(
				defineSalary.getId());
		dsObject.setRegistState(2);
		dsObject.setRewardsPunishment(productMoney);
		dsObject.setTotalMoney(totalSalar);
		this.getDefineSalaryService().saveDefineSalaryx(dsObject);
	}

	@Override
	public void saveDefineSalaryx(DefineSalary defineSalary) {
		this.getDao().saveDefineSalary(defineSalary);
	}
	

	@Override
	public String BackDefineSalary(String productName, String productYM) {
		
		/**
		 * 配料
		 */
		double PLMM=0.0;
		double shijiMonly=0.0;
		double feiliaoMonly=0.0;
		List<MaterialWages> PLlist=this.getMaterialWagesService().getMaterialWages(productYM,productName);
		int Psize=PLlist.size();
		if(Psize>0){
			List<MaterialWages> shijiList=new ArrayList<MaterialWages>();
			List<MaterialWages> feiliaoist=new ArrayList<MaterialWages>();
			for(MaterialWages mw:PLlist){
				if("实际产量".equals(mw.getProStyles())){
					shijiList.add(mw);
				}else{
					feiliaoist.add(mw);
				}
			}
			//计算实际的金额
	     int sjSize=shijiList.size();
	     if(sjSize>0){
	    List<MaterialWages> jg=new ArrayList<MaterialWages>();
			for(MaterialWages msj:shijiList){
				if(jg.size()==0){
					jg.add(msj);	
				}else{
					int judge=0;
					int bssize=jg.size();
					for(int x=0;x<bssize;x++){
				MaterialWages	msjj=jg.get(x);
					if((msjj.getDiscriminationValue()).equals(msj.getDiscriminationValue())){
						msjj.setThNums(msjj.getThNums()+msj.getThNums());
						
						jg.set(x, msjj);
						
						judge++;
					}	
					//
					}
					if(judge==0){
						jg.add(msj);
					}
					//
				}
				
			}
			
			 
		     for(MaterialWages xx:jg){
		    	 double jsuan=0.0;
		    	 System.out.println("xx.getThNums() sj="+xx.getThNums());
		    	 System.out.println("Double.parseDouble(xx.getProvideNums()) sj="+Double.parseDouble(xx.getProvideNums()));
		    	 if(xx.getThNums()>Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=(xx.getThNums()-Double.parseDouble(xx.getProvideNums()))*xx.getPoundPrice(); 
		    	 }
		    	 else if(xx.getThNums()<Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=-((Double.parseDouble(xx.getProvideNums())-xx.getThNums())*xx.getRwardPrice()); 
		    	 }
		    	
		    	 shijiMonly=shijiMonly+jsuan;	 
		     }
		     
	     }
	    
	     //废料计算
	     int fLSize=feiliaoist.size();
	     if(fLSize>0){
	    List<MaterialWages> jg=new ArrayList<MaterialWages>();
			for(MaterialWages msj:feiliaoist){
				if(jg.size()==0){
					jg.add(msj);	
				}else{
					int judge=0;
					int bssize=jg.size();
					for(int x=0;x<bssize;x++){
				MaterialWages	msjj=jg.get(x);
					if((msjj.getDiscriminationValue()).equals(msj.getDiscriminationValue())){
						msjj.setThNums(msjj.getThNums()+msj.getThNums());
						
						jg.set(x, msjj);
					
						judge++;
					}	
					//
					}
					if(judge==0){
						jg.add(msj);
					}
					//
				}
				
			}
			
			
			
		     for(MaterialWages xx:jg){
		    	 double jsuan=0.0;
		    	
		    	 if(xx.getThNums()>Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=(xx.getThNums()-Double.parseDouble(xx.getProvideNums()))*xx.getPoundPrice(); 
		    	 }
		    	 else if(xx.getThNums()<Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=-((Double.parseDouble(xx.getProvideNums())-xx.getThNums())*xx.getRwardPrice()); 
		    	 }
		    	
		    	 feiliaoMonly=feiliaoMonly+jsuan;	 
		     }
		     
	     }
	   
	     
		//-----------------------------------	
		}
		PLMM=shijiMonly+feiliaoMonly;
		/**
		 * 成型
		 */
		 double CXMM=0.0;
		 double shijiMonly1=0.0;
		 double feiliaoMonly1=0.0;
		List<MoldingWages> CXlist=this.getMoldingWagesService().getMoldingWages(productYM,productName);
		int CXize=CXlist.size();
		if(CXize>0){
			List<MoldingWages> shijiList=new ArrayList<MoldingWages>();
			List<MoldingWages> feiliaoist=new ArrayList<MoldingWages>();
			for(MoldingWages mw:CXlist){
				if("实际产量".equals(mw.getProStyles())){
					shijiList.add(mw);
				}else{
					feiliaoist.add(mw);
				}
			}
			//计算实际的金额
	     int sjSize=shijiList.size();
	     if(sjSize>0){
	    List<MoldingWages> jg=new ArrayList<MoldingWages>();
			for(MoldingWages msj:shijiList){
				if(jg.size()==0){
					jg.add(msj);	
				}else{
					int judge=0;
					int bssize=jg.size();
					for(int x=0;x<bssize;x++){
						MoldingWages	msjj=jg.get(x);
					if((msjj.getDiscriminationValue()).equals(msj.getDiscriminationValue())){
						msjj.setThNums(msjj.getThNums()+msj.getThNums());
						
						jg.set(x, msjj);
						
						judge++;
					}	
					//
					}
					if(judge==0){
						jg.add(msj);
					}
					//
				}
				
			}
			
			 
		     for(MoldingWages xx:jg){
		    	 double jsuan=0.0;
		    	 if(xx.getThNums()>Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=(xx.getThNums()-Double.parseDouble(xx.getProvideNums()))*xx.getPoundPrice(); 
		    	 }
		    	 else if(xx.getThNums()<Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=-((Double.parseDouble(xx.getProvideNums())-xx.getThNums())*xx.getRwardPrice()); 
		    	 }
		    	
		    	 shijiMonly1=shijiMonly1+jsuan;	 
		     }
		     
	     }
	     
	     //废料计算
	     int fLSize=feiliaoist.size();
	     if(fLSize>0){
	    List<MoldingWages> jg=new ArrayList<MoldingWages>();
			for(MoldingWages msj:feiliaoist){
				if(jg.size()==0){
					jg.add(msj);	
				}else{
					int judge=0;
					int bssize=jg.size();
					for(int x=0;x<bssize;x++){
						MoldingWages	msjj=jg.get(x);
					if((msjj.getDiscriminationValue()).equals(msj.getDiscriminationValue())){
						msjj.setThNums(msjj.getThNums()+msj.getThNums());
						
						jg.set(x, msjj);
						
						judge++;
					}	
					//
					}
					if(judge==0){
						jg.add(msj);
					}
					//
				}
				
			}
			
			
		     for(MoldingWages xx:jg){
		    	 double jsuan=0.0;
		    	 if(xx.getThNums()>Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=(xx.getThNums()-Double.parseDouble(xx.getProvideNums()))*xx.getPoundPrice(); 
		    	 }
		    	 else if(xx.getThNums()<Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=-((Double.parseDouble(xx.getProvideNums())-xx.getThNums())*xx.getRwardPrice()); 
		    	 }
		    	
		    	 feiliaoMonly1=feiliaoMonly1+jsuan;	 
		     }
		     
	     }
	     
	     
		//-----------------------------------	
		}
		CXMM=shijiMonly1+feiliaoMonly1;
		
		/**
		 * 印刷
		 */
		 double YSMM=0.0;
		 double shijiMonly2=0.0;
		
		List<PrintWages>  YSlist=this.getPrintWagesService().getPrintWages(productYM,productName);
		int YSize=YSlist.size();
		if(YSize>0){
			List<PrintWages> shijiList=new ArrayList<PrintWages>();
		
			for(PrintWages mw:YSlist){
				
					shijiList.add(mw);
			
			}
			//计算实际的金额
	     int sjSize=shijiList.size();
	     if(sjSize>0){
	    List<PrintWages> jg=new ArrayList<PrintWages>();
			for(PrintWages msj:shijiList){
				if(jg.size()==0){
					jg.add(msj);	
				}else{
					int judge=0;
					int bssize=jg.size();
					for(int x=0;x<bssize;x++){
						PrintWages	msjj=jg.get(x);
					if((msjj.getDiscriminationValue()).equals(msj.getDiscriminationValue())){
						msjj.setThNums(msjj.getThNums()+msj.getThNums());
					
						jg.set(x, msjj);
					
						judge++;
					}	
					//
					}
					if(judge==0){
						jg.add(msj);
					}
					//
				}
				
			}
			
			 
		     for(PrintWages xx:jg){
		    	 double jsuan=0.0;
		    	 if(xx.getThNums()>Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=(xx.getThNums()-Double.parseDouble(xx.getProvideNums()))*xx.getPoundPrice(); 
		    	 }
		    	 else if(xx.getThNums()<Double.parseDouble(xx.getProvideNums())){
		    		 jsuan=-((Double.parseDouble(xx.getProvideNums())-xx.getThNums())*xx.getRwardPrice()); 
		    	 }
		    	
		    	 shijiMonly1=shijiMonly1+jsuan;	 
		     }
		     
	     }
	     
	  
	     
		//-----------------------------------	
		}
		YSMM=shijiMonly2;	
		
		
		
		
		double total=PLMM+YSMM+CXMM;
		
		return total+"";
	}

	public ProvideRegimeService getProvideRegimeService() {
		return provideRegimeService;
	}

	public void setProvideRegimeService(
			ProvideRegimeService provideRegimeService) {
		this.provideRegimeService = provideRegimeService;
	}

	public MaterialWagesService getMaterialWagesService() {
		return materialWagesService;
	}

	public void setMaterialWagesService(MaterialWagesService materialWagesService) {
		this.materialWagesService = materialWagesService;
	}

	public MoldingWagesService getMoldingWagesService() {
		return moldingWagesService;
	}

	public void setMoldingWagesService(MoldingWagesService moldingWagesService) {
		this.moldingWagesService = moldingWagesService;
	}

	public PrintWagesService getPrintWagesService() {
		return printWagesService;
	}

	public void setPrintWagesService(PrintWagesService printWagesService) {
		this.printWagesService = printWagesService;
	}

}
